from django.shortcuts import render, HttpResponse
from .models import User
from startdjango.settings import SECRET_KEY


# Create your views here.
def index(request):
    return render(request, 'index.html')


def login(request):
    if request.method == "POST":
        name = request.POST.get('name')
        password = request.POST.get('password')
        user = User.objects.filter(name=name, password=password).first()
        if user and user.name == name and user.password == password:
            return HttpResponse('登录成功')
        else:
            return HttpResponse('登录失败')
    return render(request, 'login.html', locals())


def register(request):
    if request.method == "POST":
        name = request.POST.get('name')
        password = request.POST.get('password')
        exist_user = User.objects.filter(name=name).first()
        if exist_user and exist_user.name == name:
            return HttpResponse('用户已存在')
        new_user = User(name=name, password=password)
        new_user.save()
        return HttpResponse('注册成功')
    return render(request, 'register.html')

def test(request):
    return render(request,'test.html')